﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using webapp4U;
using webapp4U.BOL;

public partial class Usercontrol_ProductByParent : System.Web.UI.UserControl
{
    string error = "";
    string CommodKey = "";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        
        CommodKey = Globals.GetStringFromQueryString("CommodKey");        

        Pager.IndexChanged += new EventHandler(PageIndex_Changed);


        if (!Page.IsPostBack)
            this.DataBind();
    }

    private void PageIndex_Changed(Object sender, EventArgs e)
    {
        DataBind();
    }

    protected List<webapp4U.BOL.Products> GetItemsByInClassKey(string InClassKey)
    {
        List<webapp4U.BOL.Products> obj = webapp4U.BOL.Products.GetTop6Product(InClassKey, ref error);
        return obj;
    }

    public override void DataBind()
    {
        // hien thi bao nhieu hinh tren 1 trang
        Pager.PageSize = 6;
        

        List<webapp4U.BOL.InClassCategory> obj = webapp4U.BOL.InClassCategory.GetAllCategoryByCommodKey(CommodKey,Pager.PageIndex, Pager.PageSize, ref error);
        rptCategory.DataSource = obj;
        rptCategory.DataBind();


        Pager.TotalRecords = webapp4U.BOL.InClassCategory.GetCountCategoryByCommodKey(CommodKey, ref error);

        // kiem tra xem co can hien thi phan trang ko
        if (Pager.TotalRecords <= 1)
            DisplayPager.Visible = false;
        else
            DisplayPager.Visible = true;
    }
        

    protected List<webapp4U.BOL.Products> GetItemsByCateID(string InClassKey)
    {
        List<webapp4U.BOL.Products> obj = webapp4U.BOL.Products.GetTop6Product(InClassKey, ref error);
        return obj;
    }

    protected string GetPrice(float UnitPrice)
    {
        if (UnitPrice > 0)
            return UnitPrice.ToString() + " VNĐ";
        else
            return ResourceManager.GetString("Product_Price_Lienhe");
    }

    protected string GetButtonAddToCart(Double UnitPrice)
    {
        if (UnitPrice > 0)
            if (Globals.LangVietNam())
                return "<img src=" + Globals.ImagesUrl() + "AddToCart_nho.gif border=0 />";
            else
                return "<img src=" + Globals.ImagesUrl() + "AddToCart_nho_en.gif border=0 />";
        return "";
    }

    protected bool GetButtonImageAddToCart(Double UnitPrice)
    {
        if (UnitPrice > 0)
            return true;
        return false;
    }

    protected void rptCategory_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        DataList dl = e.Item.FindControl("ItemsDataList") as DataList;
        foreach (DataListItem dli in dl.Items)
        {
            webapp4U.UI.Button btnAddToCart = (webapp4U.UI.Button)dli.FindControl("btnAddToCart");
            webapp4U.UI.Button btnProductOrder = (webapp4U.UI.Button)dli.FindControl("btnProductOrder");

            //Image img = (Image)dli.FindControl("testImage");

            Label lblPrice = (Label)dli.FindControl("lblPrice");
            if (lblPrice.Text == webapp4U.ResourceManager.GetString("Product_Price_Lienhe"))
            {
                btnAddToCart.Visible = false;
                btnProductOrder.Visible = false;
                //img.Visible = false;
            }
            else
            {
                HtmlInputHidden txtItemkey = (HtmlInputHidden)dli.FindControl("txtItemkey");
                string CallBack = Page.ClientScript.GetCallbackEventReference(Page.Master.FindControl("ShoppingCart1"), "document.getElementById('" + txtItemkey.ClientID + "').value", "RateContent", "null");
                btnAddToCart.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);

                //img.Attributes["onclick"] = String.Format("javascript:{0}", CallBack);
                //img.ImageUrl = Globals.ImagesUrl() + "AddToCart_nho.gif";
            }
        }
    }

    protected void btn_ProductOrder_Click(object sender, EventArgs e)
    {
        //Products obj = Products.GetProductsByItemKey(ItemKey, ref error);
        //ShoppingCart shop = new ShoppingCart();
        //shop.AddedDate = DateTime.Now;
        //shop.ItemKey = obj.ItemKey;
        //shop.Description = obj.Description;
        //shop.UnitPrice = obj.UnitPrice;
        //shop.Quantity = 1;
        //shop.Subtotal = obj.UnitPrice;

        //List<ShoppingCart> ListShopCart = new List<ShoppingCart>();
        //if (Session["ShoppingCart"] == null)
        //{
        //    ListShopCart.Add(shop);
        //    Session["ShoppingCart"] = ListShopCart;
        //}
        //else
        //{
        //    ListShopCart = (List<ShoppingCart>)Session["ShoppingCart"];
        //    Session["ShoppingCart"] = ShoppingCart.AddItemToShoppingCart(ListShopCart, shop);
        //}

        //Response.Redirect(Globals.ApplicationPath + "Page/Products/shoppingcart.aspx?Action=DetailCart");
    }
}